﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生成绩管理系统
{
    public partial class Frmcjcx : Form
    {
        public Frmcjcx()
        {
            InitializeComponent();
        }
        DataSet ds = null;
        //学生成绩查询的代码
        public Frmcjcx(string flag)
        {
            InitializeComponent();
            if ("学生"==Constants.Userlevel )
            {//判断用户的登录身份，如果是学生，则直接显示其用户名，不允许期查询其他人的成绩
                this.TxtStuid.Text = Constants.Username;
                this.TxtStuid.Enabled = false;
            }
            if (flag=="mod")
            {//当前进行修改查询出的某条记录的操作
                this.BtnMod.Visible = true;
            }
            else if (flag=="del")
            {//当前进行删除查询的某条记录的操作
                this.BtnDel.Visible = true;
            }
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            bindDataGird();//绑定数据
        }
        public void bindDataGird()
        {
            string sno = this.TxtStuid.Text.Trim();
            string cno = this.txtKcid.Text.Trim();
            StuGradeData data = new StuGradeData();
            data.Sno = sno;//获取学号
            data.Cno = cno;//获取课程号
            try
            {
                ds = StuGradeOperation.getStuGrade(data);
                this.dataGrid1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
        public static string s, c;
        private void BtnMod_Click(object sender, EventArgs e)
        {
    
            if (this.TxtStuid.Text.ToString()==null||""==this.txtKcid.Text.ToString())
            {//必须选择某条记录
                MessageBox.Show("请选择要修改的记录！", "提示");
                return;
            }
            int result
                    = DataAccess.Result(this.TxtStuid.Text.Trim(), this.txtKcid.Text.Trim());
            if(result<=0)
            {
                MessageBox.Show("学号或课程号输入错误！", "提示");
                this.TxtStuid.Focus(); return;
            }
            else
            {
                s = this.TxtStuid.Text.Trim().ToString();
                c = this.txtKcid.Text.Trim().ToString();
                Frmaddcj frmaddcj = new Frmaddcj();
                frmaddcj.Show();//调出成绩修改界面

            }
        }

        private void BtnDel_Click(object sender, EventArgs e)
        {
            if (this.txtKcid.Text.ToString()==""||this.TxtStuid.Text.ToString()=="")
            {
                MessageBox.Show("请填入要删除的记录！", "提示");
                return;
            }
            else
            {
                if (MessageBox.Show("确认要删除吗？", "删除", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string sno = this.TxtStuid.Text.ToString();
                    string cno = this.txtKcid.Text.ToString();
                    try
                    {
                        if (StuGradeOperation.deleteStuGrade(sno, cno))
                        {//调用删除某条成绩记录的函数
                            MessageBox.Show("删除成功！", "提示");
                            bindDataGird();

                        }
                        else
                        {
                            MessageBox.Show("删除失败！", "错误");
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                        MessageBox.Show("删除失败！", "错误");
                    }
                }
            }
        }
    }
    //Constants类的定义
    public class Constants
    {
        private static string username = "";
        private static string userlevel = "";
        public static string Username//获取用户名
        {
            get { return username; }
            set { username = value; }
        }
        public static string Userlevel//获取用户身份
        {
            get { return userlevel; }
            set { userlevel = value; }
        }
    }
    //StuGradeData类的定义
    public class StuGradeData
    {
        private string sno = "";
        private string cno = "";
        private string gradepeacetime = "";
        private string gradeexpriment = "";
        private string gradelast = "";
        private string grade = "";
        public string Sno//学生的学号
        {
            get { return sno; }
            set { sno = value; }
        }
        public string Cno//课程号
        {
            get { return cno; }
            set { cno = value; }
        }
        public string Gradepeacetime//平时成绩
        {
            get { return gradepeacetime; }
            set { gradepeacetime = value; }
        }
        public string Gradeexpriment//实验成绩
        {
            get { return gradeexpriment; }
            set { gradeexpriment = value; }
        }
        public string Gradelast//期末成绩
        {
            get { return gradelast; }
            set { gradelast = value; }
        }
        public string Grade//总成绩
        {
            get { return grade; }
            set { grade = value; }
        }
    }
    //StuGradeOperation
    public class StuGradeOperation
    {
        private static DataAccess dataAccess = new DataAccess();
        public static bool inserStuGrade(StuGradeData stuGrade)
        {//插入成绩信息
            string sql = "insert into 成绩表(学号,课程编号,平时成绩,实验成绩,期末成绩,总成绩)values(" + "'" + stuGrade.Sno + "'," +
                "'"+stuGrade.Cno +"',"+
                "'" + stuGrade.Gradepeacetime + "'," +
                "'" + stuGrade.Gradeexpriment + "'," +
                "'" + stuGrade.Gradelast + "'," +
                "'" + stuGrade.Grade + "')";
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool updateStuGrade(StuGradeData stuGrade)
        {//修改成绩信息
            string sql = "update 成绩表 set 平时成绩='" +
                stuGrade.Gradepeacetime + "'," +
                "实验成绩='" + stuGrade.Gradeexpriment + "'," +
                "期末成绩='" + stuGrade.Gradelast + "'," +
                "总成绩='" + stuGrade.Grade + "'" +
                "where 学号='" + stuGrade.Sno + "' and " +
                "课程编号='" + stuGrade.Cno + "'";
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool deleteStuGrade(string sno, string cno)
        {//删除成绩信息
            string sql = String.Format("delete 成绩表 where 学号='{0}' and 课程编号='{1}'", sno, cno);
            return dataAccess.ExecuteSQL(sql);//执行sql语句

        }
        public static DataSet getStuGrade(StuGradeData stuGrade)
        {//获取查询的成绩信息
            string condition ="";//查询条件
            if (stuGrade.Sno != null && stuGrade.Sno != "")
            {
                condition += "and a.学号='" + stuGrade.Sno + "'";
            }
            if (stuGrade.Cno != null && stuGrade.Cno != "")
            {
                condition += "and b.课程编号='" + stuGrade.Cno + "'";
            }
            string sql = "select a.学号,a.姓名,c.课程编号,b.班级名称,c.平时成绩,c.实验成绩,c.期末成绩,c.总成绩 from 学生信息表 a,课程表 b,成绩表 c where c.学号=a.学号 and b.课程编号=c.课程编号  " + condition;
            return dataAccess.GetDataSet(sql, "成绩表");//执行sql语句
        }
    }
}
